//
//  LssTabbar.swift
//  freeChatting
//
//  Created by mac on 16/4/16.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
protocol LssTabbarDelegate:NSObjectProtocol{
    func clickComposeButton()
}
class LssTabbar: UITabBar {
    weak var tabbarDelegate:LssTabbarDelegate?
  override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }

  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    
//Mark:子控件的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        let margin = kScreenW / 5
        var index: CGFloat = 0
        for value in self.subviews{
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.origin.x = index * margin
                value.frame.size.width = margin
                index++
                if index == 2{
                    index++
                }
            }
            
        }
        composeButton.center.x = self.center.x
        composeButton.center.y = self.frame.size.height * 0.5
        
    }
    //Mark:composeButton的点击事件
  @objc private func composeButtonClickAction(){
    tabbarDelegate?.clickComposeButton()
    }
    //Mark:composeButton的懒加载
    
   private lazy var composeButton: UIButton = {
        let button = UIButton()
        //按钮添加点击事件
        button.addTarget(self, action: "composeButtonClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        button.sizeToFit()
        return button
    }()
    
}
